﻿using Common;
using DataLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;


namespace TicketSystem
{
    public partial class Form_CheckoutLog : Form
    {
        private TicketSystemEntities tse = new TicketSystemEntities();
        public Form_CheckoutLog()
        {
            InitializeComponent();
        }

        private void Form_CheckoutLog_Load(object sender, EventArgs e)
        {           
            var log = (from n in tse.Checkouts select new { n.CheckoutID, n.Checkout_at, n.Operators.Name }).ToList();
            dataGridView1.DataSource = log;
            dataGridView1.Columns[0].HeaderCell.Value = "ID";
            dataGridView1.Columns[1].HeaderCell.Value = "结账日期";
            dataGridView1.Columns[2].HeaderCell.Value = "操作员";
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            DateTime start = Convert.ToDateTime(Convert.ToDateTime(dTP_Start.Text).ToString("yyyy-MM-dd ") + " 00:00:00");
            DateTime end = Convert.ToDateTime(Convert.ToDateTime(dTP_End.Text).ToString("yyyy-MM-dd ") + " 23:59:59");
            var log = (from n in tse.Checkouts where n.Checkout_at >= start && n.Checkout_at <= end select new { n.CheckoutID, n.Checkout_at, n.Operators.Name }).ToList();
            dataGridView1.DataSource = log;
            dataGridView1.Columns[0].HeaderCell.Value = "ID";
            dataGridView1.Columns[1].HeaderCell.Value = "结账日期";
            dataGridView1.Columns[2].HeaderCell.Value = "操作员";
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            DateTime dtime = DateTime.Parse(dataGridView1[1, row].Value.ToString());
            DialogResult dresult = MessageBox.Show("是否打印" + dtime + "的汇总表", "打印汇总表", MessageBoxButtons.OKCancel);
            if (dresult == DialogResult.OK)
            {
                if (Common.PrintDataGridView.DefaultPrinter().Contains("EPSON"))
                {
                    DialogResult printResult = MessageBox.Show("检测到当前的默认打印机设备为针式打印机,是否需要切换", "选择打印机", MessageBoxButtons.YesNo);
                    if(printResult == DialogResult.Yes)
                    {
                        Form_SetDefaultPrinter form_setdefaultprint = new Form_SetDefaultPrinter();
                        form_setdefaultprint.Show();
                    }
                    else
                    {
                        Print_Doc(dtime);
                    }
                }
                else
                    Print_Doc(dtime);
            }
        }


        private void Print_Doc(DateTime checkoutTime)
        {
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("1", "结账时间");
            dgv.Columns.Add("2", "首道船签单");
            dgv.Columns.Add("3", "首道船非签单");
            dgv.Columns.Add("4", "进园成人票");
            dgv.Columns.Add("5", "进园学生票");
            dgv.Columns.Add("6", "二道船人数");
            dgv.Columns.Add("7", "三道船人数");
            dgv.Columns.Add("8", "首道船退票");
            dgv.Columns.Add("9", "进园成人票退票");
            dgv.Columns.Add("10", "进园学生票退票");
            dgv.Columns.Add("11", "二道船退票");
            dgv.Columns.Add("12", "三道船退票");
            dgv.Columns.Add("13", "现付总金额");
            dgv.Columns.Add("14", "退单总金额");
            dgv.Columns.Add("15", "实收总金额");//现付金额（不含签单金额）-退单金额 
            
            var SalesDoc = tse.Sales.Where(u => u.Checkout_at == checkoutTime);
            int firstID = 0;
            int endID = 0;
            try
            {
                firstID = SalesDoc.OrderBy(u => u.SaleID).First().SaleID;
                endID = firstID + SalesDoc.Count() - 1;
                //endID = SalesDoc.OrderByDescending(u => u.SaleID).First().SaleID;
            }
            catch(Exception ex) 
            {
                MessageBox.Show(ex.Message);
                firstID = 0;
                endID = 0;
            }
            int OneTotal = 0;
            int OneSignedTotal = 0;
            int OneUnSignedTotal = 0;
            int TwoTotal = 0;
            int ThreeTotal = 0;
            int ParkTotal = 0;
            int ParkSpecialTotal = 0;
            int BackOneTotal = 0;
            int BackTwoTotal = 0;
            int BackThreeTotal = 0;
            int BackParkTotal = 0;
            int BackParkSpecialTotal = 0;
            decimal PurchaseMoneyTotal = 0;
            var _type = tse.TicketTypes;
            decimal onePrice = _type.Find(1).Price;
            decimal twoPrice = _type.Find(2).Price;
            decimal threePrice = _type.Find(3).Price;
            decimal parkPrice = _type.Find(4).Price;
            decimal parkSpecialPrice = _type.Find(4).SpecialPrice;
            foreach (var sale in SalesDoc)
            {
                OneTotal += sale.TicketOne;
                OneSignedTotal += sale.SignedPeople;                
                TwoTotal += sale.TicketTwo;
                ThreeTotal += sale.TicketThree;
                ParkTotal += sale.TicketPark;
                ParkSpecialTotal += sale.TicketParkSpecial;
                PurchaseMoneyTotal += sale.PurchaseMoney;
            }
            OneUnSignedTotal = OneTotal - OneSignedTotal;//非签单人数
            var BackDoc = tse.Backs.Where(u => u.Updated_at == checkoutTime);
            decimal BackMoneyTotal = 0;
            foreach (var back in BackDoc)
            {
                BackOneTotal += back.BackOne;
                BackTwoTotal += back.BackTwo;
                BackThreeTotal += back.BackThree;
                BackParkTotal += back.BackPark;
                BackParkSpecialTotal += back.BackParkSpecial;
                BackMoneyTotal += back.BackMoney;
            }

            decimal TotalMoney = PurchaseMoneyTotal - BackMoneyTotal;
            dgv.Rows[0].Cells[0].Value = checkoutTime;
            dgv.Rows[0].Cells[1].Value = OneSignedTotal;
            dgv.Rows[0].Cells[2].Value = OneUnSignedTotal;
            dgv.Rows[0].Cells[3].Value = ParkTotal;
            dgv.Rows[0].Cells[4].Value = ParkSpecialTotal;
            dgv.Rows[0].Cells[5].Value = TwoTotal;
            dgv.Rows[0].Cells[6].Value = ThreeTotal;
            dgv.Rows[0].Cells[7].Value = BackOneTotal;
            dgv.Rows[0].Cells[8].Value = BackParkTotal;
            dgv.Rows[0].Cells[9].Value = BackParkSpecialTotal;
            dgv.Rows[0].Cells[10].Value = BackTwoTotal;
            dgv.Rows[0].Cells[11].Value = BackThreeTotal;
            dgv.Rows[0].Cells[12].Value = PurchaseMoneyTotal;
            dgv.Rows[0].Cells[13].Value = BackMoneyTotal;
            dgv.Rows[0].Cells[14].Value = TotalMoney;
            Common.PrintDataGridView.Print(dgv, "结账汇总表", "结账：" + checkoutTime + ",开票流水号区间：" + firstID + "--" + endID, 0, 0);

            //DataGridView dgv3 = new DataGridView();
            //dgv3.Columns.Add("1", "首道船现付");
            //dgv3.Columns.Add("2", "签单");
            //dgv3.Columns.Add("3", "金额");
            //dgv3.Columns.Add("4", "进园成人");
            //dgv3.Columns.Add("5", "进园学生");
            //dgv3.Columns.Add("6", "金额");
            //dgv3.Columns.Add("7", "二道船人数");
            //dgv3.Columns.Add("8", "金额");
            //dgv3.Columns.Add("9", "三道船人数");
            //dgv3.Columns.Add("10", "金额");
            //dgv3.Columns.Add("11", "合计金额");
            //string oneBackMoney = (BackOneTotal * onePrice).ToString();
            //string twoBackMoney = (BackTwoTotal * twoPrice).ToString();
            //string threeBackMoney = (BackThreeTotal * threePrice).ToString();
            //string parkBackMoney = (BackParkTotal * parkPrice).ToString();
            //string parkBackSpecialMoney = (BackParkSpecialTotal * parkSpecialPrice).ToString();
            //string parkTotalBackMoney = ((BackParkTotal * parkPrice) + (BackParkSpecialTotal * parkSpecialPrice)).ToString();
            //dgv3.Rows[0].Cells[0].Value = BackOneTotal;
            //dgv3.Rows[0].Cells[1].Value = OneSignedTotal;
            //dgv3.Rows[0].Cells[2].Value = oneBackMoney;
            //dgv3.Rows[0].Cells[3].Value = BackParkTotal;
            //dgv3.Rows[0].Cells[4].Value = BackParkSpecialTotal;
            //dgv3.Rows[0].Cells[5].Value = parkTotalBackMoney;
            //dgv3.Rows[0].Cells[6].Value = BackTwoTotal;
            //dgv3.Rows[0].Cells[7].Value = twoBackMoney;
            //dgv3.Rows[0].Cells[8].Value = BackThreeTotal;
            //dgv3.Rows[0].Cells[9].Value = threeBackMoney;
            //dgv3.Rows[0].Cells[10].Value = BackMoneyTotal;
            //Common.PrintDataGridView.Print(dgv3, "退票汇总表", "时间：" + checkoutTime, 0, 0);

            //DataGridView dgv2 = new DataGridView();
            //dgv2.Columns.Add("1", "首道船现付");
            //dgv2.Columns.Add("2", "签单");
            //dgv2.Columns.Add("3", "金额");
            //dgv2.Columns.Add("4", "进园成人");
            //dgv2.Columns.Add("5", "进园学生");
            //dgv2.Columns.Add("6", "金额");
            //dgv2.Columns.Add("7", "二道船人数");
            //dgv2.Columns.Add("8", "二金额");
            //dgv2.Columns.Add("9", "三道船人数");
            //dgv2.Columns.Add("10", "金额");
            //dgv2.Columns.Add("11", "合计金额");
            //string oneMoney = (OneUnSignedTotal * onePrice).ToString();
            //string twoMoney = (TwoTotal * twoPrice).ToString();
            //string threeMoney = (ThreeTotal * threePrice).ToString();
            //string parkMoney = (ParkTotal * parkPrice).ToString();
            //string parkSpecialMoney = (ParkSpecialTotal * parkSpecialPrice).ToString();
            //dgv2.Rows[0].Cells[0].Value = OneUnSignedTotal;
            //dgv2.Rows[0].Cells[1].Value = OneSignedTotal;
            //dgv2.Rows[0].Cells[2].Value = oneMoney;
            //dgv2.Rows[0].Cells[3].Value = ParkTotal;
            //dgv2.Rows[0].Cells[4].Value = ParkSpecialTotal;
            //dgv2.Rows[0].Cells[5].Value = parkMoney;
            //dgv2.Rows[0].Cells[6].Value = TwoTotal;
            //dgv2.Rows[0].Cells[7].Value = twoMoney;
            //dgv2.Rows[0].Cells[8].Value = ThreeTotal;
            //dgv2.Rows[0].Cells[9].Value = threeMoney;
            //dgv2.Rows[0].Cells[10].Value = PurchaseMoneyTotal;
            //Common.PrintDataGridView.Print(dgv2, "收入汇总表", "时间：" + checkoutTime, 0, 0);

            

            //DataGridView dgv4 = new DataGridView();
            //dgv4.Columns.Add("1", "入园成人");
            //dgv4.Columns.Add("2", "入园优惠");
            //dgv4.Columns.Add("3", "金额");
            //dgv4.Columns.Add("4", "二道船人数");
            //dgv4.Columns.Add("5", "金额");
            //dgv4.Columns.Add("6", "三道船人数");
            //dgv4.Columns.Add("7", "金额");
            //dgv4.Columns.Add("8", "合计金额");
            //dgv4.Rows[0].Cells[0].Value = ParkTotal;
            //dgv4.Rows[0].Cells[1].Value = ParkSpecialTotal;
            //dgv4.Rows[0].Cells[2].Value = (decimal.Parse(parkMoney) + decimal.Parse(parkSpecialMoney)).ToString();
            //dgv4.Rows[0].Cells[3].Value = TwoTotal;
            //dgv4.Rows[0].Cells[4].Value = twoMoney;
            //dgv4.Rows[0].Cells[5].Value = ThreeTotal;
            //dgv4.Rows[0].Cells[6].Value = threeMoney;
            //dgv4.Rows[0].Cells[7].Value = (decimal.Parse(parkSpecialMoney) + decimal.Parse(parkMoney) + decimal.Parse(twoMoney) + decimal.Parse(threeMoney)).ToString();
            //Common.PrintDataGridView.Print(dgv4, "颐和园入园对账汇总表", "时间" + checkoutTime, 0, 0);


            //DataGridView dgv5 = new DataGridView();
            //dgv5.Columns.Add("1", "收款金额");
            //dgv5.Columns.Add("2", "退票金额");
            //dgv5.Columns.Add("3", "实际交款金额");
            //dgv5.Rows[0].Cells[0].Value = PurchaseMoneyTotal;
            //dgv5.Rows[0].Cells[1].Value = BackMoneyTotal;
            //dgv5.Rows[0].Cells[2].Value = TotalMoney;
            //Common.PrintDataGridView.Print(dgv5, "当日缴款金额汇总表", "时间" + checkoutTime, 0, 0);
        }
    }
}
